//
//  GangCaiViewController.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/6/3.
//

#import "GangCaiViewController.h"
#import "PingTeHeaderView.h"
#import "CommonUtil.h"
#import "DrawPrizeModel.h"
#import "GangcaiChangeAlert.h"
#import "PingTeViewController.h"
#import "MemberCell.h"
#import "GangCaiHeaderView.h"
#import "ChallengeViewController.h"
#import "GangCaiSelectAlert.h"
#import "GangCaiAddAlert.h"
@interface GangCaiViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) GangCaiHeaderView * headerView;
@property (strong, nonatomic) UITableView * tableView;
@property (strong, nonatomic) NSMutableArray * models;
@property (strong, nonatomic) UIButton * bonusBtn;
@property (strong, nonatomic) UIButton * addBtn;
@end

@implementation GangCaiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(243.0, 243.0, 243.0);
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"港彩名人堂" forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"icon_mingrentang_pull"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, AD(100), AD(44));
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -AD(195));
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, AD(10));
    btn.width = AD(200);
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = btn;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bonusBtn];
    [self.view addSubview:self.addBtn];
    [self testData];
}


// 假数据
- (void)testData{
    self.models = [NSMutableArray array];
    for (NSInteger i = 0; i < 10; i++) {
        DrawPrizeModel *model = [[DrawPrizeModel alloc] init];
        if (i == 0) {
            model.showBtn = YES;
        }
        
        
        [self.models addObject:model];
    }
    [self.tableView reloadData];
}




#pragma mark - event action
- (void)btnClick:(UIButton *)sender{
    GangcaiChangeAlert *alert = (GangcaiChangeAlert *)[HudFactory creatHudWithType:@"GangcaiChangeAlert"];
    alert.currentTitle = sender.titleLabel.text;
    alert.actionBlock = ^(NSString *data) {
        NSLog(@"%@",data);
        [sender setTitle:data forState:UIControlStateNormal];
    };
    [alert show];
}


- (void)headerBtnClick:(UIButton *)sender{
    NSLog(@"%@",sender.titleLabel.text);
    if ([sender.titleLabel.text isEqualToString:@"统计"]) {
        [self.navigationController pushViewController:[StatisticsViewController new] animated:YES];
    }else if ([sender.titleLabel.text isEqualToString:@"挑战"]){
        [self.navigationController pushViewController:[ChallengeViewController new] animated:YES];
    }
    
   
}
- (void)filterBtnClick:(UIButton *)sender{
    GangCaiSelectAlert *alert = (GangCaiSelectAlert *)[HudFactory creatHudWithType:@"GangCaiSelectAlert"];
    alert.actionBlock = ^(id data) {
        NSLog(@"%@",data);
    };
    [alert show];
    [alert removeFromSuperview];
    [self.tableView addSubview:alert];
    alert.whiteView.top = self.headerView.height;
    
}
- (void)bonusbtnClick:(UIButton *)sender{
    NSLog(@"红包");
}

- (void)addbtnClick:(UIButton *)sender{
    NSLog(@"+");
    GangCaiAddAlert *alert = (GangCaiAddAlert *)[HudFactory creatHudWithType:@"GangCaiAddAlert"];
    alert.actionBlock = ^(id data) {
        NSLog(@"%@",data);
    };
    [alert show];
}



#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MemberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.isGangcai = true;
    [cell refreshWithData:indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   [self.navigationController pushViewController:[PingTeViewController new] animated:YES];


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return AD(160);
}




-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kNavBarAndStatusBarHeight)];
        [_tableView registerClass:[MemberCell class] forCellReuseIdentifier:@"cell"];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headerView;
        
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, kTabBarHeight, 0);
        _tableView.backgroundColor = [UIColor clearColor];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
//iOS11系统及以上，
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
            // Fallback on earlier versions
        }
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

-(GangCaiHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[GangCaiHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, AD(500))];
        [_headerView.subcriBtn addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView.tongjiBtn addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView.tiaozhanBtn addTarget:self action:@selector(headerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView.filterBtn addTarget:self action:@selector(filterBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        __weak typeof(self) weakSelf = self;
        _headerView.actionBlock = ^(NSString * _Nonnull title) {
            NSLog(@"%@",title);
            [weakSelf.tableView reloadData];
        };
    }
    return _headerView;
}

-(UIButton *)bonusBtn{
    if (!_bonusBtn) {
        _bonusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bonusBtn setBackgroundImage:[UIImage imageNamed:@"IMG_0743(1)"] forState:UIControlStateNormal];
        [_bonusBtn setBackgroundImage:[UIImage imageNamed:@"IMG_0743(1)"] forState:UIControlStateHighlighted];
        _bonusBtn.frame = CGRectMake(SCREEN_WIDTH-AD(80), SCREEN_HEIGHT-AD(240), AD(60), AD(60));
        [_bonusBtn addTarget:self action:@selector(bonusbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bonusBtn;
}

-(UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setImage:[UIImage imageNamed:@"icon_gangcai_add"] forState:UIControlStateNormal];
        _addBtn.frame = CGRectMake(SCREEN_WIDTH-AD(80), SCREEN_HEIGHT-AD(160), AD(60), AD(60));
        _addBtn.backgroundColor = [UIColor redColor];
        _addBtn.layer.cornerRadius = AD(30);
        _addBtn.backgroundColor = [UIColor whiteColor];
        _addBtn.layer.shadowColor = [UIColor blackColor].CGColor;
        _addBtn.layer.shadowOffset = CGSizeMake(0, 0 );
        _addBtn.layer.shadowOpacity = 0.3;
        _addBtn.layer.shadowRadius = AD(2);
        [_addBtn addTarget:self action:@selector(addbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addBtn;
}


@end
